/**
 * Copyright 2012 Roger Cockshutt
 * krakenbrau@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.parallelrl.display;

import java.util.Vector;

import com.googlecode.parallelrl.Config;
import com.googlecode.parallelrl.process.Game;
import com.googlecode.parallelrl.util.Message;
import com.googlecode.parallelrl.util.Point2D;

/**
 * @author Roger Cockshutt
 *
 */
public class GameLogger {
	
	private static GameLogger instance = null;
	
	private GameLogger() {
		size = Config.logWindowSize;
		position = Config.logWindowPosition;
	}
	
	public static GameLogger get() {
		if (instance == null) {
			instance = new GameLogger();
		}
		return instance;
	}
	
	private final Point2D size;
	private final Point2D position;
	private final Vector<Message>		buffer 	= new Vector<Message>();
	private final Vector<Message>		current	= new Vector<Message>();
	private final Vector<Message> 		messageHistory 	= new Vector<Message>();
	
	private Integer updateCount = 0;

	public void addMessage(Message message) {
		buffer.add(message);
		messageHistory.add(message);
	}
	
	public void addMessage(String message, int color) {
		addMessage(new Message(message, color));
	}
	
	private void drawCurrent() {
		int yPos = position.y;
		int xPos = position.x;
		int ySize = size.y;
		int xSize = size.x;
		Screen.get().clear(yPos, xPos, yPos + ySize, xPos + xSize);
		for (int iy = 0; iy < ySize && iy < current.size(); iy++) {
			Screen.get().puts(current.get(iy).getMessage(), iy + yPos, xPos, current.get(iy).getColor(), Colors.libTCOD.black.normal);
		}
	}
	
	public void update() {
		int yPos = position.y;
		int xPos = position.x;
		int ySize = size.y;
		int xSize = size.x;
		
		if (current.size() == 0) {
			updateCount = 0;
		}
		if (updateCount >= 10) {
			messageHistory.add(current.remove(0));
			updateCount = 5;
		}
		
		
		
		drawCurrent();
		
		while (buffer.size() > 0) {
			
			current.add(buffer.remove(0));
			
			while (current.size() > ySize) {
				messageHistory.add(current.remove(0));
				updateCount = 0;
			}
			
			if (buffer.size() >= ySize) {
				Screen.get().puts("(More...)", yPos + ySize - 1, xPos + xSize - 10, Colors.libTCOD.white.normal, Colors.libTCOD.black.normal);
				for (int iy = 0; iy < ySize; iy++) {
					messageHistory.add(current.remove(0));
					updateCount = 0;
				}
				Game.waitForKey();
			}
			drawCurrent();
		}
		updateCount++;
	}
	
}
